home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ELECTRON / PCB_DESI / 1540.ZIP / PCBCA110.ZIP / DIST.C < prev    next >
C/C++ Source or Header  |  1989-12-27  |  3KB  |  88 lines

  1. #include <stdio.h>
  2. #include "cell.h"
  3.  
  4. int GetApxDist( int, int, int, int );
  5. int CalcDist( int, int, int );
  6.  
  7. int GetApxDist ( r1, c1, r2, c2 ) /* calculate approximate distance */
  8.     int r1, c1, r2, c2;
  9.     {
  10.     register int d1, d2; /* row and column deltas */
  11.     int d0; /* temporary variable for swapping d1 and d2 */
  12.  
  13.     if ((d1 = r1-r2) < 0) /* get absolute row delta */
  14.         d1 = -d1;
  15.     if ((d2 = c1-c2) < 0) /* get absolute column delta */
  16.         d2 = -d2;
  17.     if (!d1) /* in same row? */
  18.         return( (d2*50) ); /* 50 mils per cell */
  19.     if (!d2) /* in same column? */
  20.         return( (d1*50) ); /* 50 mils per cell */
  21.     if (d1 > d2) { /* get smaller into d1 */
  22.         d0 = d1;
  23.         d1 = d2;
  24.         d2 = d0;
  25.         }
  26.     d2 -= d1; /* get non-diagonal part of approximate "route" */
  27.     return( (d1*71)+(d2*50) ); /* 71 mils diagonally per cell */
  28.     }
  29.  
  30. /* distance to go thru a cell */
  31. static int dist[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  32.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  33. /* N  */ { 50, 60, 35, 60, 99, 60, 35, 60,   12, 12 },
  34. /* NE */ { 60, 71, 60, 71, 60, 99, 60, 71,   23, 23 },
  35. /* E  */ { 35, 60, 50, 60, 35, 60, 99, 60,   12, 12 },
  36. /* SE */ { 60, 71, 60, 71, 60, 71, 60, 99,   23, 23 },
  37. /* S  */ { 99, 60, 35, 60, 50, 60, 35, 60,   12, 12 },
  38. /* SW */ { 60, 99, 60, 71, 60, 71, 60, 71,   23, 23 },
  39. /* W  */ { 35, 60, 99, 60, 35, 60, 50, 60,   12, 12 },
  40. /* NW */ { 60, 71, 60, 99, 60, 71, 60, 71,   23, 23 },
  41.  
  42. /* OT */ { 12, 23, 12, 23, 12, 23, 12, 23,   99, 99 },
  43. /* OR */ { 99, 99, 99, 99, 99, 99, 99, 99,   99, 99 }
  44.     };
  45.  
  46. /* penalty for extraneous holes and corners, scaled by sharpness of turn */
  47. static int penalty[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  48.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  49. /* N  */ {  0,  5, 10, 15, 20, 15, 10,  5,   50, 0 },
  50. /* NE */ {  5,  0,  5, 10, 15, 20, 15, 10,   50, 0 },
  51. /* E  */ { 10,  5,  0,  5, 10, 15, 20, 15,   50, 0 },
  52. /* SE */ { 15, 10,  5,  0,  5, 10, 15, 20,   50, 0 },
  53. /* S  */ { 20, 15, 10,  5,  0,  5, 10, 15,   50, 0 },
  54. /* SW */ { 15, 20, 15, 10,  5,  0,  5, 10,   50, 0 },
  55. /* W  */ { 10, 15, 20, 15, 10,  5,  0,  5,   50, 0 },
  56. /* NW */ {  5, 10, 15, 20, 15, 10,  5,  0,   50, 0 },
  57.  
  58. /* OT */ { 50, 50, 50, 50, 50, 50, 50, 50,  100, 0 },
  59. /* OR */ {  0,  0,  0,  0,  0,  0,  0,  0,    0, 0 }
  60.     };
  61.  
  62. /*
  63. ** x is the direction to enter the cell of interest.
  64. ** y is the direction to exit the cell of interest.
  65. ** z is the direction to really exit the cell, if y=FROM_OTHERSIDE.
  66. **
  67. ** return the distance of the trace through the cell of interest.
  68. ** the calculation is driven by the tables above.
  69. */
  70.  
  71. int CalcDist ( x, y, z ) /* calculate distance of a trace through a cell */
  72.     int x, y, z;
  73.     {
  74.     int adjust;
  75.  
  76.     adjust = 0; /* set if hole is encountered */
  77.     if (x == EMPTY)
  78.         x = 10;
  79.     if (y == EMPTY)
  80.         y = 10;
  81.     else if (y == FROM_OTHERSIDE) {
  82.         if (z == EMPTY)
  83.             z = 10;
  84.         adjust = penalty[x-1][z-1];
  85.         }
  86.     return( dist[x-1][y-1] + penalty[x-1][y-1] + adjust );
  87.     }
  88.